﻿Imports Excel1 = Microsoft.Office.Interop.Excel, Microsoft.VisualBasic.FileIO, PowerPoint1 = Microsoft.Office.Interop.PowerPoint, Word1 = Microsoft.Office.Interop.Word

''' <summary>
''' Represents A File\Folder and all the Stuff needed to work with it. NEEDS UPGRADING!
''' </summary>
''' <remarks></remarks>
''' <features></features>
''' <stepthrough></stepthrough>
Public Class FSObject
    Public Property Name As String
    Public Property Type As FSType
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Extension As String
    Public Property ParentApplication As String

    Public CreateMethod As Action(Of String)


    Sub New(ByVal Name As String, ByVal Extension As String, ByVal ParentApplication As String, ByVal CreateMethod As Action(Of String), Optional ByVal Type As FSType = FSType.Unknown)
        Me.Name = Name
        If Not String.IsNullOrWhiteSpace(Extension) Then If Not Extension.StartsWith(".") Then Extension = "." & Extension
        Me.Extension = Extension
        Me.ParentApplication = ParentApplication
        Me.CreateMethod = CreateMethod
        Me.Type = Type
    End Sub


End Class


Partial Class FSObject
    'This is A Factory Constructor....COOL!
    Public Enum FSType
        Unknown
        File
        Folder
        Word
        Excel
        PowerPoint
    End Enum

    ''' <summary>
    ''' Upgrade This
    ''' This is A Factory Constructor
    ''' </summary>
    ''' <param name="FSType"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <stepthrough></stepthrough>
    Public Shared Function GetObject(ByVal FSType As FSType) As FSObject
        Select Case FSType
            Case FSObject.FSType.Folder
                Return New FSObject("Folder", "", "", Sub(x) My.Computer.FileSystem.CreateDirectory(x), FSObject.FSType.Folder)
            Case FSObject.FSType.File
                Return New FSObject("File", "", "", Sub(x) IO.File.Create(x), FSObject.FSType.Unknown)
            Case FSObject.FSType.Word
                Dim Action As Action(Of String) =
                    Sub(x)
                        Dim oWord As Word1.Application, oDoc As Word1.Document, missing As Object = System.Type.Missing
                        oWord = CreateObject("Word.Application")
                        oWord.Visible = True
                        oDoc = oWord.Documents.Add
                        oDoc.SaveAs(x, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
                        oDoc.Close()
                        oDoc = Nothing
                        oWord.Quit(missing, missing, missing)
                    End Sub
                Return New FSObject("Word", ".doc", "", Action, FSObject.FSType.Word)
            Case FSObject.FSType.Excel
                Dim Action As Action(Of String) =
                    Sub(x)
                        'http://www.dreamincode.net/forums/topic/125690-create-a-new-excel-file/

                        Dim xlApp As Excel1.Application = New Excel1.Application, xlBook As Excel1.Workbook = xlApp.Workbooks.Add
                        Dim xlSheet As Excel1.Worksheet = xlBook.Worksheets.Add

                        '  xlSheet.Cells(1, 1).Value = Text1.Text  :  xlSheet.Cells(1, 2).Value = Text2.Text
                        xlSheet.SaveAs(x)
                        xlApp.Quit() : xlApp = Nothing : xlBook = Nothing : xlSheet = Nothing
                    End Sub
                Return New FSObject("Excel", "", "", Action, FSObject.FSType.Excel)
            Case FSObject.FSType.PowerPoint
                Dim Action As Action(Of String) =
                    Sub(x)
                        '?http://support.microsoft.com/kb/303717

                        Dim oApp As PowerPoint1.Application, oPres As PowerPoint1.Presentation
                        oApp = New PowerPoint1.Application() With {.WindowState = PowerPoint1.PpWindowState.ppWindowMinimized}
                        oPres = oApp.Presentations.Add
                        oPres.SaveAs(x, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Microsoft.Office.Core.MsoTriState.msoTrue)
                        oPres.Close() : oPres = Nothing : oApp.Quit() : oApp = Nothing : GC.Collect()
                    End Sub
                Return New FSObject("PowerPoint", "", "", Action, FSObject.FSType.PowerPoint)
            Case FSObject.FSType.Unknown
                Return New FSObject("Unknown", "", "", Nothing, FSObject.FSType.Unknown)
            Case Else
                Throw New ApplicationException("Invalid FSType Used")
        End Select
    End Function


End Class